package com.cpi.cornpp.monthsperiods.action;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.cpi.cornpp.common.exception.CornPPException;
import com.cpi.cornpp.creditline.services.CreditLineService;
import com.cpi.cornpp.creditline.view.CreditLineView;
import com.cpi.cornpp.monthsperiods.exception.MonthsPeriodsException;
import com.cpi.cornpp.monthsperiods.form.MonthsPeriodsForm;
import com.cpi.cornpp.monthsperiods.services.MonthsPeriodsService;
import com.cpi.cornpp.monthsperiods.view.MonthsPeriodsView;

/**
 * Months Periods Query Action
 * @author Edgar Joao
 *
 */
public class MonthsPeriodsQueryAction extends Action{
	
	/** Default forward*/
	private final static String DEFAULT = "default";
	
	
	/**
	 * Init Values for Balance
	 */
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		int initMonth = 0;
		int initYear = 0;
		int endMonth = 0;
		int endYear = 0;
		MonthsPeriodsService monthsPeriodsService = null;
		MonthsPeriodsView initMonthPeriodView = null;
		MonthsPeriodsView endMonthPeriodView = null;
		CreditLineService creditLineService = null;
		CreditLineView creditLineView = null;
		List monthsPeriodsViewsInit = null;
		List monthsPeriodsViewsEnd = null;			
		
		
		monthsPeriodsService = new MonthsPeriodsService();
		creditLineService = new CreditLineService();
		
		monthsPeriodsViewsInit = new ArrayList(0);
		monthsPeriodsViewsEnd = new ArrayList(0);
		creditLineView = new CreditLineView();
		
		try{
			
			if(form != null){
				monthsPeriodsViewsInit = monthsPeriodsService.listOfMonthsPeriods();
				monthsPeriodsViewsEnd = monthsPeriodsService.listOfMonthsPeriods();
				creditLineView = creditLineService.getCredtiLineDTO();
				((MonthsPeriodsForm)form).setCreditLineView(creditLineView);
				((MonthsPeriodsForm)form).setTotalCreditLimit(((creditLineView.getOutOfCredit()/100)*creditLineView.getLimit())+ creditLineView.getLimit());
				
				Calendar calendar = Calendar.getInstance();
				calendar.add(Calendar.MONTH, -2);
				
				initMonth = calendar.get(Calendar.MONTH);
				initYear = calendar.get(Calendar.YEAR);
				
				calendar = Calendar.getInstance();
				calendar.add(Calendar.MONTH, 6);
				
				endMonth = calendar.get(Calendar.MONTH);
				endYear = calendar.get(Calendar.YEAR);
				
				for (Iterator itr = monthsPeriodsViewsInit.iterator(); itr.hasNext(); ){
					MonthsPeriodsView monthsPeriodsView = (MonthsPeriodsView) itr.next();
					
					if ((monthsPeriodsView.getMonth() == (initMonth + 1)) && (monthsPeriodsView.getYear() == (initYear))){
						initMonthPeriodView = monthsPeriodsView;
						break;
					}
				}
				
				for (Iterator itr = monthsPeriodsViewsEnd.iterator(); itr.hasNext(); ){
					MonthsPeriodsView monthsPeriodsView = (MonthsPeriodsView) itr.next();
					
					if ((monthsPeriodsView.getMonth() == (endMonth + 1)) && (monthsPeriodsView.getYear() == (endYear))){
						endMonthPeriodView = monthsPeriodsView;
						break;
					}
				}
				
				request.setAttribute("initMonthPeriodView", (initMonthPeriodView != null) ? initMonthPeriodView : monthsPeriodsViewsInit.get(0));
				request.setAttribute("endMonthPeriodView", (endMonthPeriodView != null) ? endMonthPeriodView : monthsPeriodsViewsEnd.get(monthsPeriodsViewsEnd.size() - 1));
				
				request.setAttribute("monthsPeriodsViewsInit", monthsPeriodsViewsInit);
				request.setAttribute("monthsPeriodsViewsEnd", monthsPeriodsViewsEnd);
			}
			
		}catch(Exception exception){
			MonthsPeriodsException monthsPeriodsException=null;
			exception.printStackTrace(System.out);
			monthsPeriodsException= new MonthsPeriodsException(exception , CornPPException.LAYER_ACTION,CornPPException.ACTION_LISTS);
			throw monthsPeriodsException;
		}					
		
		return mapping.findForward(DEFAULT);
	}
}

